package com.jdym.back.controller;


import com.jdym.R;
import com.jdym.back.entity.CoBank;
import com.jdym.back.entity.PopularLoan;
import com.jdym.back.service.CoBankService;
import com.jdym.back.service.PopularLoanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping(value = "/back/PL")
public class PopularLoanController {

    @Autowired
    PopularLoanService popularLoanService;
    @Autowired
    CoBankService coBankService;
    @ResponseBody
    @RequestMapping("/getAllPopularLoan")
    public R getAllPopularLoan(){

        List<PopularLoan> popularLoans=popularLoanService.getAllPopularLoan();
        return R.ok().data("popularLoans",popularLoans);
    }

    @ResponseBody
    @DeleteMapping(value = "/delPl/{plid}")
    public R delPl(@PathVariable("plid") int plid){
        popularLoanService.getBaseMapper().deleteById(plid);
        return R.ok();
    }
@ResponseBody
 @RequestMapping("/getPLById/{plid}")
    public R getPLById(@PathVariable int plid)
 {
    PopularLoan popularLoan= popularLoanService.getPLById(plid);
    return R.ok().data("pl",popularLoan);

 }

 @ResponseBody
    @RequestMapping("/UpsavePl")
    public R UpsavePl(@RequestParam(required = false) Integer plid,@RequestParam int plCoBankId,@RequestParam String description,@RequestParam String applyRq,@RequestParam BigDecimal plRate){
     System.out.println("更新："+plid+plCoBankId);
     PopularLoan pl=new PopularLoan();
     pl.setPlCoBankId(plCoBankId);
     pl.setDescription(description);
     pl.setApplyRq(applyRq);
     pl.setPlRate(plRate);
     if(plid!=null){
         pl.setPlId(plid);
         popularLoanService.getBaseMapper().updateById(pl);
     }else {
         popularLoanService.getBaseMapper().insert(pl);
     }

     return R.ok();
 }
}
